package com.syqc.monitor.ui.realalarm

import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.base.BaseBarActivity
import com.syqc.comlib.custom.MToolbar
import com.syqc.comlib.rx.LiveDataBus
import com.syqc.comlib.rx.doAfterChanged
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.comlib.utils.loadGif
import com.syqc.comlib.utils.setResultTo
import com.syqc.comlib.utils.showToast
import com.syqc.entity.AlarmSub
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivitySubAlarm2Binding
import com.syqc.monitor.ui.alarm.ReportSetModel
import com.syqc.utils.AlarmDataUtil
import com.syqc.utils.Keys
import com.syqc.utils.ToolsUtil

/**
 *@Author Jasper
 *@Time   2020/11/30
 *@Desc   订阅报警
 */
class AlarmChooseActivity2 :
    BaseBarActivity<ActivitySubAlarm2Binding>(ActivitySubAlarm2Binding::inflate) {
    private var ids: String = ""

    //是否是安全报警
    private var isAdas = false
    private var isCar = false

    private val mAdapter by lazy { AlarmTypeAdapter2() }
    private val emptyView by lazy {
        val view = layoutInflater.inflate(
            R.layout.layout_empty_view,
            binding.recyclerAlarm,
            false
        )
        view.findViewById<ImageView>(R.id.ivNoData).loadGif(this, R.mipmap.ic_empty)

        view
    }

    override fun initView() {
        ToolbarHelper.setToolbar(
            this,
            R.string.desc_alarm_type,
            R.string.desc_com_sure,
            object : MToolbar.IToolbarCallback {
                override fun actionOnClick(action: TextView) {
                    if (ToolsUtil.isFastClick()) return
                    updateAlarms()
                }
            }
        )
        isAdas = intent.getBooleanExtra(Keys.INTENT_OTHER, false)
        isCar = intent.getBooleanExtra(Keys.INTENT_TYPE, false)
        binding.recyclerAlarm.layoutManager = LinearLayoutManager(this)
        binding.recyclerAlarm.adapter = mAdapter
        mAdapter.setEmptyView(emptyView)

        initAlarmData()
    }

    override fun setListener() {

        binding.tvAlarmAllCheck.setOnClickListener {
            binding.tvAlarmAllCheck.isSelected = true
            binding.tvAlarmAllUnCheck.isSelected = false
            checkAll()
        }
        binding.tvAlarmAllUnCheck.setOnClickListener {
            binding.tvAlarmAllCheck.isSelected = false
            binding.tvAlarmAllUnCheck.isSelected = true
            uncheckAll()
        }

        binding.tvSearch.setOnClickListener {
            val key = binding.editSearch.text.toString()
            if (key.isNullOrEmpty()) {
                showToast(R.string.hint_search_toast_alarm)
                return@setOnClickListener
            }
            binding.llAllChoose.isVisible = false
            val dataLike = AlarmDataUtil.instance.getDataLike(isAdas, key)
            if (dataLike.isEmpty()) {
                mAdapter.setEmptyView(emptyView)
            }
            mAdapter.setList(dataLike)

        }
        binding.ivClear.setOnClickListener {
            binding.editSearch.setText("")
        }
        binding.editSearch.doAfterChanged {
            binding.ivClear.isInvisible = it.isNullOrEmpty()
            if (it.isNullOrEmpty()) {
                binding.llAllChoose.isVisible = true
                mAdapter.setList(AlarmDataUtil.instance.getAllAlarms(isAdas))
            }
        }

        mAdapter.setOnItemClickListener { adapter, view, position ->
            val alarmSub = mAdapter.data[position]
            alarmSub.isChoose = !alarmSub.isChoose
            if (alarmSub.itemType == 1) {
                AlarmDataUtil.instance.setChildChoose(isAdas, alarmSub.id, alarmSub.isChoose)
            } else {
                AlarmDataUtil.instance.setPChoose(isAdas, alarmSub.pid)
            }
            binding.tvAlarmAllCheck.isSelected = AlarmDataUtil.instance.isAllChoose(isAdas)
            binding.tvAlarmAllUnCheck.isSelected = AlarmDataUtil.instance.isAllNoChoose(isAdas)
            adapter.notifyDataSetChanged()
        }
    }


    private fun initAlarmData() {
        ids = intent.getStringExtra(Keys.INTENT_DATA) ?: ""
        handleData(ids)
    }

    // 全选
    private fun checkAll() {
        AlarmDataUtil.instance.setAllChoose(isAdas, true)
        mAdapter.notifyDataSetChanged()
    }

    //全不选
    private fun uncheckAll() {
        AlarmDataUtil.instance.setAllChoose(isAdas, false)
        mAdapter.notifyDataSetChanged()
    }


    private fun handleData(ids: String) {
        AlarmDataUtil.instance.resetAlarm(isAdas)
        val alarmsByList = AlarmDataUtil.instance.getChooseByIds(isAdas, ids)
        AlarmDataUtil.instance.getChooseIds(isAdas)
        mAdapter.setList(alarmsByList)
        binding.tvAlarmAllCheck.isSelected = AlarmDataUtil.instance.isAllChoose(isAdas)
        binding.tvAlarmAllUnCheck.isSelected = AlarmDataUtil.instance.isAllNoChoose(isAdas)
    }

    private fun updateAlarms() {
        val alarmIds = AlarmDataUtil.instance.getChooseIds(isAdas)
        setResultTo(this) {
            putExtra("alarmIds", alarmIds)
        }
        if (isCar) {
            if (isAdas) {
                ReportSetModel.instance.setAdasAlarmIds(alarmIds)
            } else {
                ReportSetModel.instance.setNormalAlarmIds(alarmIds)
            }
        }
        LiveDataBus.get().with("AlarmChoose").postValue(alarmIds)
        finish()
    }
}

class AlarmTypeAdapter2 : BaseMultiItemQuickAdapter<AlarmSub, BaseViewHolder>() {
    init {
        addItemType(1, R.layout.item_alarm_type_p)
        addItemType(2, R.layout.item_alarm_choose)
    }

    override fun convert(holder: BaseViewHolder, item: AlarmSub) {

        when (getDefItemViewType(holder.layoutPosition)) {
            1 -> {
                val allChoose = holder.getView<TextView>(R.id.tvAllChoose)
                allChoose.text = item.name
                allChoose.isSelected = item.isChoose
            }

            2 -> {
                val textChildren = holder.getView<TextView>(R.id.tvItemAlarmChoose)
                textChildren.text = item.name
                textChildren.isSelected = item.isChoose
            }
        }
    }
}